/**
 * Copyright(C) 2012 Luvina Software Company
 *
 * ListUserManaged.java, 28/12/2012, bacdv
 */
package net.luvina.manageuser.managedbean;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import net.luvina.manageuser.constant.CommonConstant;
import net.luvina.manageuser.dto.UserDetailDto;
import net.luvina.manageuser.logic.MstGroupLogic;
import net.luvina.manageuser.logic.TblUserLogic;
import net.luvina.manageuser.util.CommonUtil;
import net.luvina.manageuser.util.ConfigProperties;
import net.luvina.manageuser.util.Message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * get list user from login
 *
 * @author bacdv
 *
 */
@Component("listUserManaged")
@Scope("session")
public class ListUserManaged extends UserBaseManaged implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final int STEP = 3;
    private List<SelectItem> itemGroups;
    private int currentPage;
    private String fullName;
    private int groupId;
    private int limit;
    private String message;
    private int offset;
    private String sortByCodeLevel;
    private String sortByEndDate;
    private String sortByFullName;
    private String sortType;
    private String strPaging;
    private int totalRecords;

    private List<UserDetailDto> lsUserDetailDto;
    @Autowired
    private MstGroupLogic mstGroupLogic;
    @Autowired
    private TblUserLogic tblUserLogic;

    private boolean isBack = false;

    /**
     * Constructor
     */
    public ListUserManaged() {
    }

    /**
     * create initPage of bean
     *
     * @param event
     */
    public final void initPage(PhaseEvent event) {
        if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
            this.itemGroups = mstGroupLogic.getListItemGroup(ConfigProperties
                    .getData("select_all"));
            this.doSearch();
        }
    }

    /**
     *
     * doInit
     *
     * @return
     */
    public final String doInit() {
        this.setValueDefault();
        return CommonConstant.ADM002;
    }

    /**
     * doBack
     *
     * @return CommonConstant.ADM002
     */
    public final String doBack() {
        isBack = true;
        return CommonConstant.ADM002;
    }

    /**
     * doSort
     *
     * @return CommonConstant.ADM002
     */
    public final String doSort() {
        HttpServletRequest request = (HttpServletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();

        this.sortType = request.getParameter("sortType");
        this.sortByFullName = request.getParameter("sortByFullName");
        this.sortByCodeLevel = request.getParameter("sortByCodeLevel");
        this.sortByEndDate = request.getParameter("sortByEndDate");

        if ("full_name".equals(this.sortType)) {
            this.sortByCodeLevel = CommonConstant.SORT_ASC;
            this.sortByEndDate = CommonConstant.SORT_ASC;

        } else if ("code_level".equals(this.sortType)) {

            this.sortByFullName = CommonConstant.SORT_ASC;
            this.sortByEndDate = CommonConstant.SORT_ASC;

        } else if ("end_date".equals(this.sortType)) {
            this.sortByFullName = CommonConstant.SORT_ASC;
            this.sortByCodeLevel = CommonConstant.SORT_ASC;
        } else {
            this.sortByFullName = CommonConstant.SORT_ASC;
            this.sortByCodeLevel = CommonConstant.SORT_ASC;
            this.sortByEndDate = CommonConstant.SORT_ASC;
        }

        return CommonConstant.ADM002;

    }

    /**
     * search in database
     *
     * @return String ADM002
     */
    public final String doSearch() {
        HttpServletRequest request = (HttpServletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();
        String strCurrentPage = request.getParameter("page");
        if ((!this.isBack)) {
            this.currentPage = 0;
        }
        this.isBack = false;
        if (strCurrentPage != null && !"".equals(strCurrentPage)) {
            this.currentPage = Integer.valueOf(strCurrentPage);
        }
        if (this.currentPage <= 0) {
            this.currentPage = 1;
        }
        this.limit = CommonUtil.getLimit();
        this.offset = CommonUtil.getOffset(currentPage, limit);
        this.totalRecords = tblUserLogic.getTotalRecords(groupId, fullName);

        if (this.totalRecords > 0) {
            this.lsUserDetailDto = this.tblUserLogic.getListUser(offset, limit,
                    groupId, fullName, sortType, sortByFullName,
                    sortByCodeLevel, sortByEndDate);
            strPaging = CommonUtil.paging(totalRecords, limit, currentPage,
                    CommonConstant.ADM002);
            message = "";
        } else {
            this.lsUserDetailDto = this.tblUserLogic.getListUser(offset, limit,
                    groupId, fullName, sortType, sortByFullName,
                    sortByCodeLevel, sortByEndDate);
            this.message = Message.getString("MSG005");
            strPaging = "";
        }
        return CommonConstant.ADM002;
    }

    /**
     *
     */
    private final void setValueDefault() {
        this.fullName = "";
        this.groupId = 0;
        this.sortType = "";
        this.sortByCodeLevel = "";
        this.sortByEndDate = "";
        this.sortByFullName = "";
    }

    /**
     * @return the itemGroups
     */
    public List<SelectItem> getItemGroups() {
        return itemGroups;
    }

    /**
     * @param itemGroups
     *            the itemGroups to set
     */
    public void setItemGroups(List<SelectItem> itemGroups) {
        this.itemGroups = itemGroups;
    }

    /**
     * @return the currentPage
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * @param currentPage
     *            the currentPage to set
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * @return the fullName
     */
    public String getFullName() {
        return fullName;
    }

    /**
     * @param fullName
     *            the fullName to set
     */
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    /**
     * @return the groupId
     */
    public int getGroupId() {
        return groupId;
    }

    /**
     * @param groupId
     *            the groupId to set
     */
    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    /**
     * @return the limit
     */
    public int getLimit() {
        return limit;
    }

    /**
     * @param limit
     *            the limit to set
     */
    public void setLimit(int limit) {
        this.limit = limit;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message
     *            the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the offset
     */
    public int getOffset() {
        return offset;
    }

    /**
     * @param offset
     *            the offset to set
     */
    public void setOffset(int offset) {
        this.offset = offset;
    }

    /**
     * @return the sortByCodeLevel
     */
    public String getSortByCodeLevel() {
        return sortByCodeLevel;
    }

    /**
     * @param sortByCodeLevel
     *            the sortByCodeLevel to set
     */
    public void setSortByCodeLevel(String sortByCodeLevel) {
        this.sortByCodeLevel = sortByCodeLevel;
    }

    /**
     * @return the sortByEndDate
     */
    public String getSortByEndDate() {
        return sortByEndDate;
    }

    /**
     * @param sortByEndDate
     *            the sortByEndDate to set
     */
    public void setSortByEndDate(String sortByEndDate) {
        this.sortByEndDate = sortByEndDate;
    }

    /**
     * @return the sortByFullName
     */
    public String getSortByFullName() {
        return sortByFullName;
    }

    /**
     * @param sortByFullName
     *            the sortByFullName to set
     */
    public void setSortByFullName(String sortByFullName) {
        this.sortByFullName = sortByFullName;
    }

    /**
     * @return the sortType
     */
    public String getSortType() {
        return sortType;
    }

    /**
     * @param sortType
     *            the sortType to set
     */
    public void setSortType(String sortType) {
        this.sortType = sortType;
    }

    /**
     * @return the strPaging
     */
    public String getStrPaging() {
        return strPaging;
    }

    /**
     * @param strPaging
     *            the strPaging to set
     */
    public void setStrPaging(String strPaging) {
        this.strPaging = strPaging;
    }

    /**
     * @return the totalRecords
     */
    public int getTotalRecords() {
        return totalRecords;
    }

    /**
     * @param totalRecords
     *            the totalRecords to set
     */
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    /**
     * @return the lsUserDetailDto
     */
    public List<UserDetailDto> getLsUserDetailDto() {
        return lsUserDetailDto;
    }

    /**
     * @param lsUserDetailDto
     *            the lsUserDetailDto to set
     */
    public void setLsUserDetailDto(List<UserDetailDto> lsUserDetailDto) {
        this.lsUserDetailDto = lsUserDetailDto;
    }

    /**
     * @return the mstGroupLogic
     */
    public MstGroupLogic getMstGroupLogic() {
        return mstGroupLogic;
    }

    /**
     * @param mstGroupLogic
     *            the mstGroupLogic to set
     */
    public void setMstGroupLogic(MstGroupLogic mstGroupLogic) {
        this.mstGroupLogic = mstGroupLogic;
    }

    /**
     * @return the tblUserLogic
     */
    public TblUserLogic getTblUserLogic() {
        return tblUserLogic;
    }

    /**
     * @param tblUserLogic
     *            the tblUserLogic to set
     */
    public void setTblUserLogic(TblUserLogic tblUserLogic) {
        this.tblUserLogic = tblUserLogic;
    }

    /**
     * @return the isBack
     */
    public boolean isBack() {
        return isBack;
    }

    /**
     * @param isBack
     *            the isBack to set
     */
    public void setBack(boolean isBack) {
        this.isBack = isBack;
    }

}
